package com.nbsaas.boot.app.ext.resource;

import com.nbsaas.boot.app.api.apis.AppRoleApi;
import com.nbsaas.boot.app.api.apis.AppUserRoleApi;
import com.nbsaas.boot.app.api.domain.request.AppUserRoleRequest;
import com.nbsaas.boot.app.api.domain.response.AppRoleResponse;
import com.nbsaas.boot.app.api.domain.response.AppUserRoleResponse;
import com.nbsaas.boot.app.ext.apis.AppUserRoleExtApi;
import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.ResponseObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class AppUserRoleExtResource implements AppUserRoleExtApi {

    @Resource
    private AppUserRoleApi appUserRoleApi;

    @Resource
    private AppRoleApi appRoleApi;

    @Transactional
    @Override
    public ResponseObject<AppUserRoleResponse> create(AppUserRoleRequest request) {
        if (request.getRole() == null) {
            return ResponseObject.error("角色id不能为空");
        }
        AppRoleResponse role = appRoleApi.oneData(Filter.eq("id", request.getRole()));
        if (role==null){
            return ResponseObject.error("角色不存在");
        }
        request.setApp(role.getApp());
        return appUserRoleApi.create(request);
    }
}
